package com.neusoft.SPNursingHome.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.SPNursingHome.po.ClientCheckInDto;
import com.neusoft.SPNursingHome.po.Client;
import com.neusoft.SPNursingHome.po.ClientWithCareLevel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Map;
import java.util.List;

@Mapper
public interface ClientDao extends BaseMapper<Client> {
    @Update("UPDATE client " +
            "SET care_admin_id = NULL " +
            "WHERE client_id = #{clientId}")
    int unassignById(@Param("clientId") Integer clientId);

//    @Select("SELECT * FROM client WHERE client_name LIKE CONCAT('%', #{clientName}, '%') AND client_type = #{careType}")
//    List<Client> searchByNameAndCareType(@Param("clientName") String clientName, @Param("careType") String careType);

//    @Select({
//            "SELECT c.*, cl.care_level_name",
//            "FROM client c",
//            "LEFT JOIN care_level cl ON c.care_level_id = cl.care_level_id",
//            "WHERE c.client_type LIKE CONCAT('%', #{careType}, '%')",
//            "AND c.client_name LIKE CONCAT('%', #{clientName}, '%')"
//    })
//    List<ClientWithCareLevel> searchByNameAndCareType(@Param("clientName") String clientName, @Param("careType") String careType);


    @Select("SELECT " +
            "    c.client_id, c.client_name, c.client_type, c.date_of_birth, c.client_gender, " +
            "    c.client_card_number, c.blood_type, c.family_member, c.client_phone, c.client_age," +
            "    cir.contract_end_time " +
            "FROM " +
            "    client c " +
            "LEFT JOIN " +
            "    (SELECT client_id, contract_end_time FROM check_in_record " +
            "     WHERE (client_id, check_in_time) IN " +
            "           (SELECT client_id, MAX(check_in_time) FROM check_in_record GROUP BY client_id)" +
            "    ) cir ON c.client_id = cir.client_id " +
            "WHERE " +
            "    c.client_id = #{clientId}")
    ClientCheckInDto findClientDetailsForUpdate(Integer clientId);

     // 根据健康管家ID分页查询其负责的客户列表
    @Select("<script>" +
            "SELECT * FROM client " + // 直接查询Client表的所有字段
            "<where> " +
            "    client_type = '护理' AND client_status = 1 " +
            "    <if test='query.adId != null'> " +
            "        AND care_admin_id = #{query.adId} " + // 根据传入的管家ID (adId) 筛选
            "    </if> " +
            "    <if test='query.clientName != null and query.clientName != \"\"'> " +
            "        AND client_name LIKE CONCAT('%', #{query.clientName}, '%') " +
            "    </if> " +
            "</where> " +
            "ORDER BY client_id ASC" +
            "</script>")
    IPage<Client> findClientsByCarerId(IPage<Client> page, @Param("query") Map<String, Object> query);

    /**
     * 新增方法：根据房间ID查询该房间内所有状态正常的客户信息
     * @param roomId 房间ID
     * @return 客户列表
     */
    @Select("SELECT c.* FROM client c " +
            "JOIN bed b ON c.bed_id = b.bed_id " +
            "WHERE b.room_id = #{roomId} AND c.client_status = 1")
    List<Client> findClientsByRoomId(@Param("roomId") String roomId);

    @Select({
            "SELECT c.*, cl.care_level_name",
            "FROM client c",
            "LEFT JOIN care_level cl ON c.care_level_id = cl.care_level_id",
            "WHERE c.client_type LIKE CONCAT('%', #{careType}, '%')",
            "AND c.client_name LIKE CONCAT('%', #{clientName}, '%')",
            "LIMIT #{offset}, #{size}"
    })
    List<ClientWithCareLevel> searchByNameAndCareType(@Param("clientName") String clientName, @Param("careType") String careType, @Param("offset") long offset, @Param("size") long size);

    @Select({
            "SELECT COUNT(*)",
            "FROM client c",
            "LEFT JOIN care_level cl ON c.care_level_id = cl.care_level_id",
            "WHERE c.client_type LIKE CONCAT('%', #{careType}, '%')",
            "AND c.client_name LIKE CONCAT('%', #{clientName}, '%')"
    })
    int countSearchByNameAndCareType(@Param("clientName") String clientName, @Param("careType") String careType);


    // 健康管家下的客户列表查询
    @Select({
            "SELECT c.*, cl.care_level_name",
            "FROM client c",
            "LEFT JOIN care_level cl ON c.care_level_id = cl.care_level_id",
            "WHERE c.client_type LIKE CONCAT('%', #{careType}, '%')",
            "AND c.client_name LIKE CONCAT('%', #{clientName}, '%')",
            "AND c.care_admin_id = #{adId}",
            "LIMIT #{offset}, #{size}"
    })
    List<ClientWithCareLevel> searchByNameAndCareTypeAndAdId(@Param("clientName") String clientName, @Param("careType") String careType, @Param("adId") Integer adId, @Param("offset") long offset, @Param("size") long size);

    @Select({
            "SELECT COUNT(*)",
            "FROM client c",
            "LEFT JOIN care_level cl ON c.care_level_id = cl.care_level_id",
            "WHERE c.client_type LIKE CONCAT('%', #{careType}, '%')",
            "AND c.care_admin_id = #{adId}",
            "AND c.client_name LIKE CONCAT('%', #{clientName}, '%')"
    })
    int countSearchByNameAndCareTypeAndAdId(@Param("clientName") String clientName, @Param("careType") String careType,@Param("adId") Integer adId);
}
